package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.util.PageResult;
import com.atguigu.jxc.vo.GoodsByNameAndTypeIdVo;
import com.atguigu.jxc.vo.GoodsListVo;
import com.atguigu.jxc.vo.SaveStockVO;
import com.atguigu.jxc.vo.SearchVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public PageResult  listInventory(GoodsListVo goodsListV) {
        Integer page = goodsListV.getPage();
        if (page ==null) page =1;
        Integer rows = goodsListV.getRows();
        if (rows == null) rows =10;
        PageHelper.startPage(page,rows);
        Page<Goods> pages  = goodsDao.listInventory(goodsListV.getGoodsTypeId(),goodsListV.getCodeOrName());
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    @Override
    public PageResult goodsListByNameAndTypeId(GoodsByNameAndTypeIdVo goodsByNameAndTypeIdVo) {
        Integer page = goodsByNameAndTypeIdVo.getPage();
        Integer rows = goodsByNameAndTypeIdVo.getRows();
        String goodsName = goodsByNameAndTypeIdVo.getGoodsName();
        Integer goodsTypeId = goodsByNameAndTypeIdVo.getGoodsTypeId();
        if (page ==null) page=1;
        if (rows==null) rows=10;
        Page<Goods> pages  = goodsDao.goodsListByNameAndTypeId(goodsName,goodsTypeId);
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    @Override
    public void saveGoods(Integer goodsId, Goods goods) {
        if (goodsId == null){
            goodsDao.saveGoods(goods);
        }else {
            goodsDao.updateGoods(goodsId,goods);
        }
    }

    @Override
    public Boolean deleteGoodsById(Integer goodsId) {
        Integer status = goodsDao.getStateByGoodsId(goodsId);
        if (status == 0 ){
            goodsDao.deleteGoodsById(goodsId);
            return true;
        }
        return false;
    }

    @Override
    public PageResult getNoInventoryQuantity(SearchVo searchVo) {
        Integer page = searchVo.getPage();
        Integer rows = searchVo.getRows();
        String nameOrCode = searchVo.getNameOrCode();
        PageHelper.startPage(page,rows);
        Page<Goods> result = goodsDao.getNoInventoryQuantity(nameOrCode);
        return new  PageResult(result.getTotal(),result.getResult());
    }

    @Override
    public PageResult getHasInventoryQuantity(SearchVo searchVo) {
        Integer page = searchVo.getPage();
        Integer rows = searchVo.getRows();
        String nameOrCode = searchVo.getNameOrCode();
        PageHelper.startPage(page,rows);
        Page<Goods> result = goodsDao.getHasInventoryQuantity(nameOrCode);
        return new  PageResult(result.getTotal(),result.getResult());
    }

    @Override
    public void saveStock(SaveStockVO saveStockVO) {
        Integer goodsId = saveStockVO.getGoodsId();
        Integer inventoryQuantity = saveStockVO.getInventoryQuantity();
        double purchasingPrice = saveStockVO.getPurchasingPrice();
        if (goodsId>0 && inventoryQuantity>0 && purchasingPrice>0) {
            goodsDao.saveStock(saveStockVO);
        }
    }

    @Override
    public Boolean deleteStock(Integer goodsId) {
        Integer status = goodsDao.getStateByGoodsId(goodsId);
        if (status == 0){
            goodsDao.deleteStock(goodsId);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<Goods> listAlarm() {
        return goodsDao.listAlarm();
    }
}
